#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QHBoxLayout>
#include <QMessageBox>
#include <QDebug>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    m_qipan(new qipan(this)),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    setFixedSize(800,680);

    m_logic = new GameLogic;
    int boardSize = 0;
    m_qipan->getBoardInfo(boardSize);
    m_logic->setQipanBoard(m_qipan->getBoardInfo(boardSize),boardSize);

    /*设置布局*/
    QWidget * centerWidget = new QWidget(this);
    setCentralWidget(centerWidget);
    QVBoxLayout * layout = new QVBoxLayout(centerWidget);
    layout->addWidget(m_qipan);

    connect(m_qipan,&qipan::chessClick,this,&MainWindow::handleChessClickSlot);
    connect(m_logic,&GameLogic::AiPosition,this,&MainWindow::handleChessPosSlot);
}

///*人机移动*/
//void MainWindow::computerMove()
//{
//    qDebug()<<"选择下在哪个位置呢"<<endl;
//    int boardSize = m_qipan->getBoardSize();
//    for(int row = 0;row <boardSize;row++)
//    {
//        for(int col = 0;col <boardSize;col++)
//        {
//            if()
//            {

//            }
//        }
//    }

//}

void MainWindow::handleChessClickSlot(int row,int col)
{
    m_qipan->setBoardInfo(row,col,PLAYER);
    m_qipan->update();
    if(m_logic->checkWin(row,col,PLAYER,5) == true)
    {
        /*玩家获胜*/
        QMessageBox::information(this,"游戏结束","玩家获胜");
    }
    else
    {
        m_logic->computerMove();

//        // AI下棋
//        int aiRow = -1, aiCol = -1;
////        m_logic->algorithmAi(aiRow, aiCol);  // 获取AI的下棋位置
//        if (aiRow != -1 && aiCol != -1)
//        {
//            m_qipan->setBoardInfo(aiRow, aiCol, COMPUTER);  // 设置AI棋子
//            m_qipan->update();  // 更新棋盘显示
//            // 检查AI是否获胜
//            if (m_logic->checkWin(aiRow, aiCol, COMPUTER, 5))
//            {

//                QMessageBox::information(this, "游戏结束", "AI获胜");
//            }
//        }
    }
}

void MainWindow::handleChessPosSlot(int row, int col)
{
    m_qipan->setBoardInfo(row,col,COMPUTER);
    /*更新棋盘*/
    m_qipan->update();

    if(m_logic->checkWin(row,col,COMPUTER,5) == true)
    {
        QMessageBox::information(this,"游戏结束","ai获胜");
    }
}

MainWindow::~MainWindow()
{
    delete ui;
}


